﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IEnumberableExtensions
{
    public static class Extensions
    {
        public static decimal Sum<T>(this IEnumerable<T> collection)
        {
            decimal sum = 0M;
            foreach (var item in collection)
            {
                decimal number = decimal.Parse(item.ToString());
                sum += number;
            }
            return sum;
        }

        public static decimal Product<T>(this IEnumerable<T> collection)
        {
            decimal product = 1M;
            foreach (var item in collection)
            {
                decimal number = decimal.Parse(item.ToString());
                product *= number;
            }
            return product;
        }

        public static T Min<T>(this IEnumerable<T> collection)
            where T: IComparable
        {
            if (collection.Count<T>() < 1)
            {
                throw new ArgumentException("The collection is empty!");
            }
            T min = collection.First<T>();
            foreach (var item in collection)
            {
                if (item.CompareTo(min) < 0)
                {
                    min = item;
                }
            }
            return min;
        }

        public static T Max<T>(this IEnumerable<T> collection)
            where T : IComparable
        {
            if (collection.Count<T>() < 1)
            {
                throw new ArgumentException("The collection is empty!");
            }
            T max = collection.First<T>();
            foreach (var item in collection)
            {
                if (item.CompareTo(max) > 0)
                {
                    max = item;
                }
            }
            return max;
        }

        public static decimal Average<T>(this IEnumerable<T> collection)
        {
            decimal sum = 0M;
            foreach (var item in collection)
            {
                decimal number = decimal.Parse(item.ToString());
                sum += number;
            }
            decimal average = sum / collection.Count<T>();
            return average;
        }
    }

    class IEnumberableExtensions
    {
        static void Main(string[] args)
        {
            List<double> numbers = new List<double>();
            for (double i = 1; i < 5; i++)
            {
                numbers.Add(i/10);
            }
            Console.WriteLine(numbers.Sum());
            Console.WriteLine(numbers.Product());
            Console.WriteLine(numbers.Min());
            Console.WriteLine(numbers.Max());
            Console.WriteLine(numbers.Average());
        }
    }
}
